package com.hzy.potrace.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.blankj.utilcode.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by huzongyao on 18-1-10.
 */

public class WebViewActivity extends AppCompatActivity {

    public static final String INTENT_WEB_VIEW_URL = "INTENT_WEB_VIEW_URL";
    public static final String INTENT_WEB_VIEW_TITLE = "INTENT_WEB_VIEW_TITLE";

    @BindView(R.id.web_web_view)
    WebView mWebWebView;
    @BindView(R.id.web_progress)
    ProgressBar mWebProgress;
    private ActionBar mActionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        mActionBar = getSupportActionBar();
        if (mActionBar != null) {
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }
        mWebProgress.setVisibility(View.GONE);
        mWebProgress.setMax(100);
        initWebPage();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mWebWebView != null && mWebWebView.canGoBack()) {
            mWebWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebPage() {
        String url = getIntent().getStringExtra(INTENT_WEB_VIEW_URL);
        String title = getIntent().getStringExtra(INTENT_WEB_VIEW_TITLE);
        if (!StringUtils.isTrimEmpty(title)) {
            setTitle(title);
        }
        WebSettings webSettings = mWebWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebWebView.setHorizontalScrollBarEnabled(false);
        mWebWebView.setWebViewClient(getWebViewClient());
        mWebWebView.setWebChromeClient(getWebChromeClient());
        if (!StringUtils.isTrimEmpty(url)) {
            mWebWebView.loadUrl(url);
        }
    }

    private WebChromeClient getWebChromeClient() {
        return new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                setTitle(title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                mWebProgress.setProgress(newProgress);
                mWebProgress.setVisibility(newProgress >= 100 ? View.GONE : View.VISIBLE);
            }
        };
    }

    private WebViewClient getWebViewClient() {
        return new WebViewClient() {
        };
    }
}
